<?php

namespace Trawind\Basics\Traits\Transform;


use Trawind\ConfigCenter\Helpers\ConfigHelper;
use Trawind\Helpers\ArrHelper;

trait TransformView
{

    public function view()
    {
        switch (request('view')) {
            case 'tree':
                return $this->viewOfTree();
            case 'group':
                return $this->viewOfGroup(request('keyBy'));
            case 'sum':
                return $this->viewOfSum();
            case 'statistic':
                return $this->viewOfStatistic();
            default:
                return $this->viewOfDefault();
        }
    }

    public function viewOfTree($skipPresenter = false)
    {
        $modelData = $this->fillTransformData($this->all());
        $data = $this->skipParserResult($modelData, $skipPresenter)['data'];
        if (!$data) {
            return [];
        }
        $minPid = min(array_column($data, 'parent_id'));
        $data = ArrHelper::tree($data, $minPid);
        if (request('isRootNode')) {
            array_unshift($data, $this->skipParserResult($this->model->fill($this->model->getTreeTopNode()))['data']);
        }
        return compact('data');
    }


    public function viewOfGroup(string $index)
    {
        $allData = $this->skipParserResult($this->fillTransformData($this->all()));
        $data = [];
        foreach ($allData['data'] as $value) {
            $data[$value[$index]][] = $value;
        }
        return compact('data');
    }


    public function viewOfDefault(bool $skipPresenter = false, ?int $limit = null, $columns = ['*'])
    {
        is_null($limit) and $limit = request('pageSize');

        $isPaginate = is_null($limit) || $limit > 0;
        if ($isPaginate && is_null($limit)) {
            $limit = ConfigHelper::resolve('page_size');
        }

        $data = $isPaginate ? $this->paginate($limit, $columns) : $this->all($columns);
        return $this->skipParserResult($this->fillTransformData($data), $skipPresenter);
    }

    public function viewOfSum(bool $skipPresenter = false, ?int $limit = null, $columns = ['*'])
    {
        $this->applyCriteria();
        $this->applyScope();
        $filter = request()->get('filter');
        if (!$filter) {
            return [];
        }
        $filterData = explode(',', $filter);
        $queryStr = '';
        foreach ($filterData as $value) {
            $queryStr .= 'sum('.$value.') as '.$value.',';
        }
        $queryStr = trim($queryStr, ',');

        $groupBy = request()->get('groupBy');
        if ($groupBy) {
            $groupByData = explode(',', $groupBy);
            $queryStr .= ','.$groupBy;
            $data = $this->getModel()
                ->select(\DB::raw($queryStr))
                ->groupBy($groupByData)
                ->get()
                ->toArray();
        } else {
            $data = $this->getModel()
                ->select(\DB::raw($queryStr))
                ->first()
                ->toArray();
        }
        return ['data' => $data];
    }

    public function viewOfStatistic(bool $skipPresenter = false, ?int $limit = null, $columns = ['*'])
    {
        $this->applyCriteria();
        $this->applyScope();
        $data = $this->getModel()
            ->select(\DB::raw('count(id) as count'))
            ->first()
            ->toArray();
        return ['data' => $data];
    }

}
